﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CivilleWP7.CivilleDataServiceReference;
using System.Data.Services.Client;
using CivilleWP7.Civille.Source;
using CivilleWP7.Views.Controls;
using System.Windows.Media.Imaging;

namespace CivilleWP7.Views.Pages
{
    public partial class MyFriendsPage : PhoneApplicationPage
    {
        private List<CivilleUser> AllUsers = new List<CivilleUser>();
        private List<int> Friends = new List<int>();
        private bool IsLightTheme;
        public MyFriendsPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MyFriendsPage_Loaded);
            setBackground();
        }
        void checkThemeColor()
        {
            var visibility = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
            if (visibility == Visibility.Visible)
                IsLightTheme = true;
            else
                IsLightTheme = false;
        }
        void setBackground()
        {
            checkThemeColor();
            ImageBrush myBrush = new ImageBrush();
            if (IsLightTheme)
                myBrush.ImageSource = new BitmapImage(new Uri("/Resources/Images/Backgroundwhite.jpg", UriKind.Relative));
            else
                myBrush.ImageSource = new BitmapImage(new Uri("/Resources/Images/Background.jpg", UriKind.Relative));
            LayoutRoot.Background = myBrush;
        }

        void MyFriendsPage_Loaded(object sender, RoutedEventArgs e)
        {
            FriendList.Items.Clear();
            CivilleDataServiceConnector.SetUsersQueryParameters(UserContext.CurrentUser.ID);
            CivilleDataServiceConnector.GetUsersQuery.BeginExecute(OnGetAllUsersCompleted, CivilleDataServiceConnector.GetUsersQuery);
        }

        private void OnGetAllUsersCompleted(IAsyncResult result)
        {
            Dispatcher.BeginInvoke(() =>
                {
                    DataServiceQuery<CivilleUser> query = result.AsyncState as DataServiceQuery<CivilleUser>;
                    foreach (CivilleUser user in query.EndExecute(result))
                    {
                        AllUsers.Add(user);
                    }

                    CivilleDataServiceConnector.SetUsersToUsersQueryParameters(UserContext.CurrentUser.ID);
                    CivilleDataServiceConnector.GetUsersToUsersQuery.BeginExecute(OnGetFriendsCompleted, CivilleDataServiceConnector.GetUsersToUsersQuery);
                });
        }

        private void OnGetFriendsCompleted(IAsyncResult result)
        {
            Dispatcher.BeginInvoke(() =>
                {
                    DataServiceQuery<CivilleUserToCivilleUser> query = result.AsyncState as DataServiceQuery<CivilleUserToCivilleUser>;
                    foreach (CivilleUserToCivilleUser friend in query.EndExecute(result))
                    {
                        Friends.Add(friend.FriendID);
                    }

                    foreach (CivilleUser user in AllUsers)
                    {
                        if (Friends.Contains(user.ID))
                        {
                            CustomListItem listItem = new CustomListItem();
                            listItem.TitleTxt.Text = user.FirstName;
                            listItem.DescriptionTxt.Text = "Points: " + user.Points;
                            listItem.CheckMarkImage.Source = new BitmapImage(new Uri((user.Logo == null || user.Logo.Equals("")) ? "/CivilleWP7;component/Resources/Images/User-icon.png" : user.Logo, UriKind.RelativeOrAbsolute));
                            listItem.CheckMarkImage.Width = 50;
                            listItem.Tag = user;
                            listItem.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(listItem_Tap);
                            FriendList.Items.Add(listItem);
                        }
                    }
                });
        }

        void listItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            CivilleUser user = (CivilleUser)(sender as CustomListItem).Tag;
            FriendPage.User = user;
            NavigationService.Navigate(new Uri(string.Format("/Views/Pages/FriendPage.xaml"), UriKind.Relative));
        }
    }
}